Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ch/psestimate memory #91

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Ch/psestimate memory #91

wants to merge 16 commits into from

Conversation

cahofer
Copy link

@cahofer cahofer commented May 6, 2020

Monte Carlo Fisher matrix estimation for m-mode formalism for large telescope classes.

  • fix(psestimation): refactering the functions generate() and q_estimator() such they can be reused in psmc.MonteCarloXLarge.
  • feat(psmc.MonteCarloXLarge):
    a class that estimates the Fisher matrix with Monte Carlo simulations when memory demand is high. Features:
    • clarray for power spectrum bands is now an MPIarray distributed over bands. Fixes memory
      issues.
    • the main function generate() divides all m-modes into chunks, so that each parallel process
      receives one m-mode at at time.
    • the function recv_send_data() manages sending and receiving of m-modes from rank to
      rank. As such, each rank with a sub range of bands is subject to every m-mode.
    • q_estimator() calculates the quadratic estimation under the assumption the q-array is a
      distributed MPIArray over bands

@cahofer
Copy link
Author

cahofer commented Jul 9, 2020

Hey @jrs65! I added Tristan and Simon to the review. Simon is probably most familiar next to you with the Fisher estimation code, and Tristan is just a really good code reviewer. Let me know if there is someone else I could add to help with this. I would like to get this going and merged!

Copy link
Contributor

@sjforeman sjforeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me! (However, I'm not an expert code reviewer like @jrs65 and @tristpinsm , so it might be a good idea to have one of them skim through it before the final merge.)

My comments are all minor, and should be easily resolvable.

drift/core/psmc.py Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
drift/core/psestimation.py Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
drift/core/psmc.py Show resolved Hide resolved
return recv_buffer

def split_single(self, m, size):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring would be useful here, to describe in words what this function does

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Need feedback if it is comprehensible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use this routine instead of mpiutil.partition_list()?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want every rank to work with one m-mode, with partition list it's not guaranteed that i-th partition will have be length = size, where size is the number of total MPI processes running.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think you could accomplish this with

mpiutil.partition_list(
    np.range(self.telescope.mmax + 1), mpiutil.rank, (self.telescope.mmax + 1) // size
)

or something similar. However, if your code works, I'm not sure if it's worth the effort to change it...

Copy link
Contributor

@jrs65 jrs65 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good, I'm glad to be finally getting this one merged.

I left a few comments, I'll have a look again when the current round of Simon's suggestions and my own are addressed. Does this actually run though? It seems like there's a few things which just shouldn't actually work.

drift/core/psmc.py Outdated Show resolved Hide resolved
drift/core/psmc.py Outdated Show resolved Hide resolved
drift/core/psmc.py Outdated Show resolved Hide resolved
drift/core/psestimation.py Outdated Show resolved Hide resolved
else:
y0 = x0
y2 = x2

return x2, y2

def q_estimator(self, mi, vec1, vec2=None, noise=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that you have an entirely new implementation of q_estimator in your new class, why does the old one need to get changed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to factor out the part of the code that projects from KL-basis to sky basis so that it can be reused as a method in the future.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and the new PS-code would get just like a big spaghetti code. Tried to factor out as much as I could.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, makes sense to me

@cahofer
Copy link
Author

cahofer commented Dec 5, 2020

This is looking good, I'm glad to be finally getting this one merged.

I left a few comments, I'll have a look again when the current round of Simon's suggestions and my own are addressed. Does this actually run though? It seems like there's a few things which just shouldn't actually work.

Yeah it's been running for half a year now...

@cahofer
Copy link
Author

cahofer commented Dec 5, 2020

I did the changes you requested. @sjforeman @jrs65. I will run the code now and blacken before I commit and push the branch to git.

@lgtm-com
Copy link

lgtm-com bot commented Feb 2, 2021

This pull request introduces 3 alerts and fixes 1 when merging 9f71ec6 into 94f0ece - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Unused import
  • 1 for Variable defined multiple times

fixed alerts:

  • 1 for Unused local variable

if mi < (self.telescope.mmax + 1):
self.q_estimator(mi, vec1)

# TO DO: Calculate q_a for noise power (x0^H N x0 = |x0|^2)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added her a TODO item - at the moment we have no capability of simulating a noise bias.
On a note:

  • the original psestimation code doesn't estimate a noise bias for a auto power spectrum (why?)
  • Is x0^2 (=normalized KL-modes) supposed to be the noise bias? According to the m-mode paper see equation (91) - I thought you need to sandwich the estimator between realizations of white noise to get b_a.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, and the only that noise is set to True is in the Crosspower class and there it should be zero, so I don't see the point why it's even calculated?!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment to self and anyone else reading: We won't save noise in KL-basis to q-estimator. IMO it really doesn't need to be there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused by these comments. Isn't the noise bias estimated by the combination of fisher_bias and gen_sample?

I guess the problem is that the noiseonly argument isn't set to True when zero_mean == False which it should be?

axis=0,
).astype(np.float64)

def noise_bias(self, mi, vec1, vec2=None):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this method for the future, in case we ever want to have a routine to calculate what Richard called the noise bias.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scrap that. I don't think we need it.

request.Wait()

# Fill recv buffer with messages
recv_buffer[slc] = bi
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, I have been getting a warning that this should be recv_buffer[tuple(slc)] instead of recv_buffer[slc]. Any opinions?

Copy link
Author

@cahofer cahofer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, this is the most up to date PR for the multi parallelized Fisher code. It is ready for a second round. I left some comments concerning calculating a noise bias.
The new code as it is works at the moment only for

  • auto correlation Fisher errors
  • when no noise bias is calculated (but neither did the old psestimation code.)

@cahofer
Copy link
Author

cahofer commented Feb 2, 2021

Oh - and for some reason the black check is failing right now even though I reformatted the files with black - so I am not sure whats going on there.

@lgtm-com
Copy link

lgtm-com bot commented Feb 2, 2021

This pull request fixes 2 alerts when merging c72303e into 94f0ece - view on LGTM.com

fixed alerts:

  • 2 for Unused local variable

Copy link
Contributor

@sjforeman sjforeman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a few minor comments, but I will punt some of the more technical points to @jrs65

drift/core/psestimation.py Show resolved Hide resolved
drift/core/beamtransfer.py Outdated Show resolved Hide resolved
else:
y0 = x0
y2 = x2

return x2, y2

def q_estimator(self, mi, vec1, vec2=None, noise=False):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, makes sense to me

drift/core/psestimation.py Show resolved Hide resolved
@@ -81,7 +85,7 @@ def _work_fisher_bias_m(self, mi):
x = self.gen_sample(mi, n)
qa[:, s:e] = self.q_estimator(mi, x)

ft = np.cov(qa)
# ft = np.cov(qa)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this could be removed completely

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, I will get rid of it... I just kept it because it was Richard's comments.

drift/core/psmc.py Show resolved Hide resolved
drift/core/psmc.py Show resolved Hide resolved
return recv_buffer

def split_single(self, m, size):

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use this routine instead of mpiutil.partition_list()?

@jrs65
Copy link
Contributor

jrs65 commented May 18, 2021

@cahofer it looks like there's a few more comments being worked on in here, is that right?

def q_estimator(self, mi, vec1, vec2=None, noise=False):
"""Estimate the q-parameters from given data (see paper).

def project_vector_kl_to_sky(self, mi, vec1):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as before: should this go in drift.core.kltransform instead of drift.core.psestimation?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants